<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

use think\Model;

class Rechange extends Model{
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    /**
     * @param array $where
     * @param int $pageSize
     * @param string $field
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where= [], $pageSize=10){
        $list = $this
            ->with('mch')
            ->order('create_time desc')
            ->where($where)
            ->paginate($pageSize);
        return $list;
    }

    public function mch(){
        return $this->hasOne('Mch','id','mc_id')
            ->bind(['username','name']);
    }

    /**
     * @param $id
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getListByMcId($id){
        $list = $this
            ->with('mch')
            ->order('create_time desc')
            ->where('type','eq',2)
            ->where('status','eq',2)
            ->where('mc_id','eq',$id)
            ->limit(10)
            ->select();
        return $list;
    }

    /**
     * 格式时间
     * @param $value
     * @return false|string
     */
    public function getFinishTimeAttr($value){
        return  $value?date('Y-m-d H:i:s',$value):'--';
    }
}